import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QLabel,
    QLineEdit,
    QPushButton,
    QVBoxLayout,
    QHBoxLayout,
    QGridLayout,
    QSpacerItem,
    QSizePolicy,
)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer, QDateTime, Qt
from util.config import Config
from PyQt5.QtGui import QPixmap, QPalette, QBrush, QFont
import os 


class MainScreen(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        

    def init_ui(self):
        self.setWindowTitle("登录界面")
        self.setGeometry(0, 0, 680, 400)
        #self.setFixedSize(680, 500)  # 设置窗口大小为400x300
        

        # 初始化配置
        self.config = Config()

        # 设置背景图片
        self.setAutoFillBackground(True)
        palette = self.palette()
        background = QPixmap(self.config.LOGIN_BG)
        palette.setBrush(QPalette.Background, QBrush(background.scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)))
        
        self.setPalette(palette)

        # 创建顶部区域topview
        topview = QWidget(self)
        #topview.setFixedHeight(50)  # 设置顶部区域的固定高度

        # 设置顶部区域的布局
        top_layout = QVBoxLayout(topview)
        top_layout.setGeometry(QtCore.QRect(0, 0, 150, 30))
        
        # time_label：显示当前时间，并且距离左边20px
        self.time_label = QLabel(topview)
        #self.time_label.setGeometry(QtCore.QRect(0, 0, 80, 30))
        self.time_label.setFont(QFont("Arial", 10))
        #self.time_label.setStyleSheet("color: white;")
        self.time_label.setAlignment(Qt.AlignLeft)
        self.time_label.setStyleSheet("""
        QLabel {
            background: transparent;   /*背景透明 */
            color: white;  /* 文本颜色 */
            margin-top:-5px;
            margin-left:60px;
        }
    """)
        self.time_label.setText('当前时间')
        self.update_time()  # 初始化时间显示
        timer = QTimer(self)
        timer.timeout.connect(lambda: self.update_time())  # 定时更新时间
        timer.start(1000)  # 每秒更新时间
        
        # 设置布局
        top_layout.addWidget(self.time_label)
        top_layout.setSpacing(10)  # 设置组件之间的间距
        
        # 表单容器
        self.form_container = QWidget(self)
        self.form_container.setMaximumWidth(400)
        
        self.form_container.setStyleSheet("""
            QWidget {
                border-radius: 10px;
                background-image: url('views/assets/login_bg2.png'); /* 替换为你的图片路径 */
                background-repeat: no-repeat;          /* 不重复 */
                background-position: center;          /* 居中显示 */
               
                
                /*image: url(views/assets/login_bg2.png);
                background-color:rgba(0,0,0,0);    */
            }
        """)


        
        # 主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)  # 去掉主布局的边距
        main_layout.setSpacing(0)  # 去掉组件之间的间隙
        main_layout.addWidget(topview)  # 添加顶部区域
        main_layout.addStretch()  # 占位符，保持时间标签在顶部

        

        self.setLayout(main_layout)
        

        
    def update_time(self):
        """更新时间"""
        current_time = QDateTime.currentDateTime()
        time_str = current_time.toString("yyyy-MM-dd HH:mm:ss")
        self.time_label.setText(time_str)
        
    def update_time(self):
        """更新顶部时间显示"""
        current_time = QDateTime.currentDateTime()
        time_str = current_time.toString("yyyy-MM-dd HH:mm:ss")
        self.time_label.setText(time_str)

    def resizeEvent(self, event):
        """调整背景图片大小"""
        palette = self.palette()
        background = QPixmap(self.config.LOGIN_BG)
        palette.setBrush(QPalette.Background, QBrush(background.scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)))
        self.setPalette(palette)
        
        super().resizeEvent(event)
        
    def login_event(self):
        print('login_event')


if __name__ == "__main__":
    app = QApplication(sys.argv)
    login_screen = LoginScreen()
    login_screen.show()
    sys.exit(app.exec_())
